package com.icez.base_util_library

import android.content.Context
import android.content.Intent
import android.net.Uri
import java.lang.Exception

/**
 *  分享工具类,facebook只能使用官方SDK
 * @author icez
 */
class ShareUtil {
    companion object {
        val instance: ShareUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ShareUtil()
        }

        val OPENLINETOTEXT = "https://line.me/R/share?text="

        val MESSENGERPKG = "com.facebook.orca"

        val WHATSAPPPKG = "com.whatsapp"

        val INSTAGRAMPKG = "com.instagram.android"
    }

    /**
     *  分享文本支持分享的APP
     * @param context 上下文
     * @param text 文本
     * @param shareAppPkg 指定分享的APK包名
     * @param mShareAppType 分享的app
     */
    public fun textToSystem(
        context: Context,
        text: String?,
        shareAppPkg: String? = null,
        mShareAppType: ShareAppType? = ShareAppType.SYSTEM
    ): Boolean {
        var intent: Intent? = null
        var mShareAppPkg = shareAppPkg
        when (mShareAppType) {
            ShareAppType.SYSTEM -> {
                intent = Intent().apply {
                    action = Intent.ACTION_SEND
                    putExtra(Intent.EXTRA_TEXT, text)
                    type = "text/plain"
                }
            }
            ShareAppType.MESSENGER -> {
                intent = Intent().apply {
                    action = Intent.ACTION_SEND
                    if (mShareAppPkg.isNullOrEmpty()) {
                        mShareAppPkg = MESSENGERPKG
                    }
                    `package` = mShareAppPkg
                    putExtra(Intent.EXTRA_TEXT, text)
                    type = "text/plain"
                }
            }
            ShareAppType.INSTAGRAM -> {
                intent = Intent().apply {
                    action = Intent.ACTION_SEND
                    if (mShareAppPkg.isNullOrEmpty()) {
                        mShareAppPkg = INSTAGRAMPKG
                    }
                    `package` = mShareAppPkg
                    putExtra(Intent.EXTRA_TEXT, text)
                    type = "text/plain"
                }
            }
            ShareAppType.EMAIL -> {
                intent = Intent().apply {
                    action = Intent.ACTION_SENDTO
                    data = Uri.parse("mailto:")
                    putExtra(Intent.EXTRA_SUBJECT, "")
                    putExtra(Intent.EXTRA_TEXT, text)
                }
            }
            ShareAppType.LINE -> {
                var uri: Uri = Uri.parse(OPENLINETOTEXT + text)
                intent = Intent().apply {
                    data = uri
                    action = Intent.ACTION_VIEW
                }
            }
            ShareAppType.MSG -> {
                intent = Intent().apply {
                    action = Intent.ACTION_SENDTO
                    data = Uri.parse("smsto:")
                    putExtra("sms_body", text)
                }
            }
            ShareAppType.WHATSAPP -> {
                intent = Intent().apply {
                    action = Intent.ACTION_SEND
                    if (mShareAppPkg.isNullOrEmpty()) {
                        mShareAppPkg = WHATSAPPPKG
                    }
                    `package` = mShareAppPkg
                    putExtra(Intent.EXTRA_TEXT, text)
                    type = "text/plain"
                }
            }
        }

        return try {
            context.startActivity(intent)
            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }

    }


    enum class ShareAppType {
        SYSTEM,//系统分享
        MESSENGER,//Messenger分享
        LINE,//line分享
        WHATSAPP,//Whatsapp分享
        INSTAGRAM,//Instragram分享
        MSG,//短信分享
        EMAIL//游戏分享
    }
}